package com.ypf.survey.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value="/")
    public String index(Model model) {
        return home(model);
    }
	
	@RequestMapping(value="/home")
    public String home(Model model) {
		return "home";
    }
	
	@RequestMapping(value="/scriptcam.swf", method=RequestMethod.GET)
	public void getSwf(HttpServletResponse response, HttpServletRequest request, String ID) {
		String marketPath = request.getSession().getServletContext().getRealPath("/static/swf");
		String fileName = "scriptcam.swf";
		handleFile(response, fileName, marketPath);
	}

	private void handleFile(HttpServletResponse response, String fileName, String imagePath) {
		response.setContentType("application/x-shockwave-flash");
		OutputStream output  = null;
		FileInputStream input = null;
		File file = new File(imagePath, fileName);
		try{
			output = response.getOutputStream();
			input = new FileInputStream(file);
			byte[] buffer = new byte[(int) file.length()];
			input.read(buffer);
			response.setContentLength(buffer.length);
			output.write(buffer);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				input.close();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
